//
//  ClassifyOneViewController.m
//  UI_xiangmu
//
//  Created by dllo on 15/4/25.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ClassifyOneViewController.h"
#import "AFHTTPRequestOperationManager.h"
#import "UIImageView+WebCache.h"
#import "ClassifyCollectionReusableView.h"
#import "detailCamereViewController.h"
@interface ClassifyOneViewController ()

@property(nonatomic,retain)NSMutableArray *modelArr;

@end

@implementation ClassifyOneViewController

-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.modelArr = [NSMutableArray array];
    }
    [self data];
    return self;
}
-(void)data
{
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=updateTime&need_sort=1" parameters:self success:^(AFHTTPRequestOperation *operation, id responseObject) {
        self.modelArr = responseObject;
        [self.myCollectionView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"解析失败");
    }];
    
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UICollectionViewFlowLayout *flowlayout=[[UICollectionViewFlowLayout alloc]init];
    flowlayout.itemSize=CGSizeMake(100 , 180);
    //最小列间距
    flowlayout.minimumInteritemSpacing=1;
    //最小行间距
    flowlayout.minimumLineSpacing=1;
    flowlayout.scrollDirection=UICollectionViewScrollDirectionVertical;
    flowlayout.sectionInset=UIEdgeInsetsMake(10, 10, 15,15);
    self.myCollectionView=[[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)  collectionViewLayout:flowlayout] ;
    self.myCollectionView.backgroundColor=[UIColor whiteColor];
    self.myCollectionView.dataSource=self;
    self.myCollectionView.delegate=self;
    self.myCollectionView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.myCollectionView];
    [self.myCollectionView release];
//    注册cell
    [self.myCollectionView registerClass:[ClassifyOneCell class] forCellWithReuseIdentifier:@"reuse1"];
 [self.myCollectionView registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head"];
 [self.myCollectionView registerClass:[ClassifyCollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"foot"];
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{   NSLog(@"%ld",self.modelArr.count);
    return self.modelArr.count;
}

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return [[self.modelArr[section] objectForKey:@"list"] count];
}

-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{ if(collectionView == self.myCollectionView){
    CGSize size = CGSizeMake(240, 25);
    return size;}
else{
    CGSize size = CGSizeMake(0, 0);
    return size;
}
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    ClassifyCollectionReusableView *headview;
    if ([kind isEqual:UICollectionElementKindSectionHeader]) {
        headview = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"head" forIndexPath:indexPath];
        headview.backgroundColor = [UIColor yellowColor];
        NSString *str = [NSString stringWithFormat:@"%@",[self.modelArr[indexPath.section] objectForKey:@"name"]];
        NSLog(@"%@",str);
        [headview setLabelText:str];
        return headview;
    }else{
        return nil;
    }
}



-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    NSMutableDictionary *dic = self.modelArr[indexPath.section];
    NSMutableArray *arr = [dic objectForKey:@"list"];
    ClassifyOneCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"reuse1" forIndexPath:indexPath];
    NSString *str = [arr[indexPath.row] objectForKey:@"cover"];
    NSURL *url = [NSURL URLWithString:str];
    [cell.myimage sd_setImageWithURL:url];
    NSString *str1 = [arr[indexPath.row]objectForKey:@"cur_num"];
    NSString *str2 = [NSString stringWithFormat:@"更新至%@集",str1];
    cell.jishulabel.text = str2;
    NSString *str3 = [arr[indexPath.row] objectForKey:@"name"];
    cell.mylabel.text = str3;
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    detailCamereViewController *de = [[detailCamereViewController alloc] init];
    NSMutableArray *arr = [self.modelArr[indexPath.section] objectForKey:@"list"];
    de.canshu = [arr[indexPath.row] objectForKey:@"video_id"];
    de.canshu = [NSString stringWithFormat:@"http://manhua.haomee.cn/?m=Api&ios=23&pad=0&a=getInfo&video_id=%@&uid=(null)",de.canshu];
    [self.navigationController pushViewController:de animated:YES];
    [de release];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
